package com.whc.user.config;


import com.whc.user.mapper.UserMapper;
import com.whc.user.service.UserService;
import com.whc.user.util.BloomFilterUtil;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class BloomFilterInit {
    @Resource
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;

    @PostConstruct//初始化白名单数据
    public void init()
    {
        List<Integer> allUserId = userMapper.getAllUserId();
        for(Integer userId:allUserId){
            //hash函数算出index
            String uid=BloomFilterUtil.whiteUserHead+userId;
            int hashValue = Math.abs(uid.hashCode());
            long index = (long) (hashValue % Math.pow(2, 32));

            //设置redis里面bitmap对应坑位，该有值设置为1
            redisTemplate.opsForValue().setBit(BloomFilterUtil.whiteUserHead,index,true);
        }

    }

}
